package com.codeworker.validationdemo.controller;

import com.codeworker.validationdemo.bean.Page;
import com.codeworker.validationdemo.bean.ReqBody;
import com.codeworker.validationdemo.bean.Student;
import com.codeworker.validationdemo.bean.Students;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Description: web层测试
 *
 * @author code-worker
 * @date 2021/09/25 10:11
 */
@RestController
public class TestController {

    @PostMapping("/student")
    public String checkStudentInfo(@Valid Student student, BindingResult result){
        if (result.hasErrors()) {
            for(ObjectError error : result.getAllErrors()){
                return error.getDefaultMessage();
            }
        }
        return "success";
    }

    @PostMapping("/students")
    public String checkStudentsInfo(@Valid @RequestBody Students students, BindingResult result){
        if (result.hasErrors()) {
            for(ObjectError error : result.getAllErrors()){
                return error.getDefaultMessage();
            }
        }
        return "success";
    }

    @PostMapping("/student/encapsulation")
    public String checkStudentInfoEnc(@Valid @RequestBody ReqBody<Student> student, BindingResult result){
        if (result.hasErrors()) {
            for(ObjectError error : result.getAllErrors()){
                return error.getDefaultMessage();
            }
        }
        return "success";
    }

    @PostMapping("/students/encapsulation")
    public String checkStudentsInfoEnc(@Valid @RequestBody ReqBody<Students> students, BindingResult result){
        if (result.hasErrors()) {
            for(ObjectError error : result.getAllErrors()){
                return error.getDefaultMessage();
            }
        }
        return "success";
    }

    @PostMapping("/students/encapsulation/page")
    public String checkStudentsInfoEncWithPage(@Valid @RequestBody ReqBody<Page<Students>> students, BindingResult result){
        if(result.hasErrors()){
            for (ObjectError error : result.getAllErrors()) {
                return error.getDefaultMessage();
            }
        }
        return "success";
    }

}
